﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using Telerik.Windows.Documents.Layout;
using Telerik.Windows.Documents.Model;
using Telerik.Windows.Examples.RichTextBox.Parsers;
using System.Xml;
using NetBay.Core.BusinessEntities.Workflow.Rules;

namespace RulesGenerator
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        #region Constants

        private static readonly string XamlResource = "SampleData/SampleXAML.txt";

        #endregion

        #region Fields

        private RadDocument XamlDocument;

        #endregion

        #region Methods

        void SyntaxHighlighTextBox_Loaded(object sender, RoutedEventArgs e)
        {
            this.LoadSampleXamlDocument();
        }

        private void LoadSampleXamlDocument()
        {
            //XmlDocument doc = new XmlDocument();
            //doc.Load(@"c:\temp\validation.xml");

            Rules r = Rules.LoadFromFile(@"c:\temp\Rules.xml");


            //if (this.XamlDocument == null)
            //{
            //    Stream xamlStream = Application.GetResourceStream(
            //        GetResourceUri(XamlResource)).Stream;

            //    using (StreamReader reader = new StreamReader(xamlStream))
            //    {
            //        this.XamlDocument = CreateFormattedDocument(reader.ReadToEnd(), ".xaml");
            //    }
            //}

            //this.Editor.Document = this.XamlDocument;
            //this.Editor.Document.ShowFormattingSymbols = false;
            //this.Editor.UpdateEditorLayout();
        }



        private RadDocument CreateFormattedDocument(string text, string fileFormat)
        {
            RadDocument document = new RadDocument();
            document.LayoutMode = DocumentLayoutMode.Flow;
            document.SectionDefaultPageMargin = new Padding(25);

            Section section = new Section();
            document.Sections.Add(section);

            Tokenizer tokenizer = new Tokenizer();
            List<Token> tokens = tokenizer.TokenizeCode(text, fileFormat);

            Paragraph currentParagraph = new Paragraph();
            currentParagraph.SpacingAfter = 0;
            section.Blocks.Add(currentParagraph);
            foreach (Token token in tokens)
            {
                string[] lines = Regex.Split(token.Value, DocumentEnvironment.NewLine);

                bool createParagraph = false;
                foreach (string line in lines)
                {
                    if (createParagraph)
                    {   
                        currentParagraph = new Paragraph();
                        currentParagraph.SpacingAfter = 0;
                        section.Blocks.Add(currentParagraph);
                    }
                    createParagraph = true;

                    if (!string.IsNullOrEmpty(line))
                    {
                        Span span = token.GetSpanStyle();
                        span.Text = line;
                        currentParagraph.Inlines.Add(span);
                    }
                }
            }
            
            return document;
        }

        private static Uri GetResourceUri(string resource)
        {
            AssemblyName assemblyName = new AssemblyName(typeof(MainWindow).Assembly.FullName);
            string resourcePath = "/" + assemblyName.Name + ";component/" + resource;

            Uri resourceUri = new Uri(resourcePath, UriKind.Relative);

            return resourceUri;
        }

        #endregion
    }
}
